package com.db.server;
/**
 * 
 * 经纪人服务类
 * 
 */

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


import com.db.curd.BaseDAO;
import com.db.dao.JJRDao;
import com.db.dao.McmDao;
import com.db.sql.SqlMapFactory;
import com.mcm.bean.*;
import com.microhifo.bean.*;
import com.tools.JsonTool;

public class JJRService {
	
	private Map<String,String> sqlMap=null;
	
	public JJRService(){
		this.sqlMap = SqlMapFactory.getInstance().getSqlMap();
	}
	
	
	//  find_juser_by_page
	public List<JUser> getByPage(int offset,int pageSize){
	try{
		List<JUser> result= null;
		String sql = SqlMapFactory.getInstance().getSqlMap().get("one_find_juser_by_page");
		JJRDao dao = JJRDao.getInstance();
		
		result =  dao.querySome(sql, new Integer[]{offset*pageSize,pageSize},JUser.class);
		return result;
	}catch(Exception e){
		return null;
	}
	}

	// 获取所有高级经纪人 尽量用分页的方法
	@Deprecated
	public List<JUser> getAllAdvance(){
		List<JUser> result= null;
		
		return result;
	}
	
	// 根据openID获取V5esbCustomerCustomer
	public V5esbCustomerCustomer getByOpenId(String openId){
		try{
			McmDao dao = McmDao.getInstance();
			String sql = SqlMapFactory.getInstance().getSqlMap().get("two_find_cid_by_openId");
			V5esbCustomerCustomer data = (V5esbCustomerCustomer) dao.queryOne(sql, new String[]{openId},V5esbCustomerCustomer.class);
			
			return data;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		
	}
	
	
	
	
	// 根据SourceCode获取获取上级V5esbCustomerCustomer  two_find_father_by_sourceCode
	public V5esbCustomerCustomer findFather(String sourceCode){
		try{
			McmDao dao =McmDao.getInstance();
			String sql = SqlMapFactory.getInstance().getSqlMap().get("two_find_father_by_sourceCode");
			V5esbCustomerCustomer data = (V5esbCustomerCustomer) dao.queryOne(sql, new String[]{sourceCode},V5esbCustomerCustomer.class);	
			return data;
		}catch(Exception e){
			return null;
		}
	}
	
	
	public List<V5esbCustomerCustomer> findBranch(String sourceCode){
		List<V5esbCustomerCustomer> result = new ArrayList<V5esbCustomerCustomer>();
		try{
			boolean flag = true;
			int i=0;
			String code = sourceCode;
			String temp="";
			while(flag== true){
				i=i+1;
				V5esbCustomerCustomer data = this.findFather(code);
				if(data == null || i>10){
					flag = false;
				}else{
					code = data.getSourceCode();
					temp = temp+"--"+code;
					result.add(data);
				}
				
			}
			System.out.println(i+"--"+temp);
			
			return result;
		}catch(Exception e){
//			System.out.println("Exception !"+"--"+sourceCode);
			return null;
		}
		
	}
	
	
	public static void main(String[] args) throws SQLException{
		List<JUser> result= null;
		JJRService js = new JJRService();
		result = js.getByPage(0, 100);	
		System.out.println(JsonTool.obj2json(result));
		
		System.out.println(JsonTool.obj2json(js.getByOpenId("o7D-hswWxFMrudyUHl-c2g2RTX9A")));
		
		
		
		System.out.println("--start--");
		for(JUser data:result){
			V5esbCustomerCustomer temp = js.getByOpenId(data.getOpenId());
			String r =JsonTool.obj2json(temp);
			System.out.println(r);
			
			List<V5esbCustomerCustomer> result2 = null;
			System.out.println(data.getName());
			try{
				V5esbCustomerCustomer result3 = js.getByOpenId(temp.getOpenId());
				if(result3==null)
					continue;
				result2 = js.findBranch(result3.getSourceCode());
				if(result!=null)
					System.out.println(JsonTool.obj2json(result2));
				else
					continue;
			}catch(Exception e){
				continue;
			}
	
		}
		
		System.out.println("----END----");
		
//		V5esbCustomerCustomer result= null;
//		JUserService js = new JUserService();
//		result = js.getIdByOpenId("o7D-hs-ROgancCLxl8D_geSdawCo");	
//		System.out.println(JsonTool.obj2json(result));
		
//		V5esbCustomerCustomer result= null;
//		JUserService js = new JUserService();
//		result = js.findFather("C001");	
//		System.out.println(JsonTool.obj2json(result));
		

		
		
	


		
		
		
		
	}

}
